import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue'),
  },
  {
    path: '/front',
    name: 'Front',
    redirect:"/front/home",
    component: () => import('../views/front/Front.vue'),
    children: [
      { path: 'home', name: 'FrontHome', component: () => import('../views/front/Home.vue'),},
      { path: 'person', name: 'FrontPerson', component: () => import('../views/front/Person.vue'),},
      { path: 'article', name: 'FrontArticle', component: () => import('../views/front/Article.vue'),},
      { path: 'articleDetail', name: 'FrontArticleDetail', component: () => import('../views/front/ArticleDetail.vue'),},
      { path: 'articleEdit', name: 'FrontArticleEdit', component: () => import('../views/front/ArticleEdit.vue'),},
      { path: 'collect', name: 'FrontCollect', component: () => import('../views/front/Collect.vue'),},
      { path: 'message', name: 'FrontMessage', component: () => import('../views/front/Message.vue'),},
      { path: 'user', name: 'FrontUser', component: () => import('../views/front/PersonDetail.vue'),}
    ]
  },

]
// 提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({
    mode:'history',
    base: process.env.BASE_URL,
    routes
  })
}



// 刷新页面会导致重置路由
export const setRoutes  = () => {
  const storeMenus = localStorage.getItem("menus")
  // console.log(localStorage.getItem("menus"))
  if (storeMenus) {
    // 获取当前路由对象名称数组
    const currentRouterNames = router.getRoutes().map(v => v.name)
    // console.log("index.js")
    // console.log(currentRouterNames)
    if (!currentRouterNames.includes("Manage")) {
      // 动态添加到现在路由对象区
      const manageRoute = {path: '/',name: 'Manage', component: () => import('../views/Manage.vue'), redirect:"/home", children: [
          { path: '/person', name: 'Person', meta: {title: '个人信息99'}, component: () => import('../views/Person.vue') }
        ]}
      const menus = JSON.parse(storeMenus)
      menus.forEach(item => {
        if (item.path) { // 当且仅当path不为空时，才去设置路由
          let itemMenu = { path: item.path.replace("/", ""), name: item.name, meta: {title: item.name}, component: () => import('../views/' + item.pagePath +'.vue')}
          manageRoute.children.push(itemMenu)
        } else if(item.children.length) {
          item.children.forEach(item => {
            if (item.path) {
              let itemMenu = { path: item.path.replace("/", ""), name: item.name, meta: {title: item.name}, component: () => import('../views/' + item.pagePath +'.vue')}
              manageRoute.children.push(itemMenu)
            }
          })
        }
      })
      router.addRoute(manageRoute)
    }
  }
}


const router = new VueRouter({
  mode: 'history',
  routes
})
// 重置就再设置路由
setRoutes()

router.beforeEach((to,from,next) =>{

  //未找到路由
  if (!to.matched.length) {
    const storeMenus =localStorage.getItem("menus")
    if (storeMenus) {
      next("/404")
    } else {
      // 跳回登录页面
      next("/front")
    }
  }
  // 有路由的情况放行
  next()

})

export default router
